package com.mall.business.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.mall.common.utils.DateUtils;
import com.mall.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TPinkMapper;
import com.mall.business.domain.TPink;
import com.mall.business.service.ITPinkService;

/**
 * 拼团Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class TPinkServiceImpl extends ServiceImpl<TPinkMapper,TPink>  implements ITPinkService
{
    @Autowired
    private TPinkMapper tPinkMapper;

    /**
     * 查询拼团
     *
     * @param id 拼团主键
     * @return 拼团
     */
    @Override
    public TPink selectTPinkById(String id)
    {
        return tPinkMapper.selectTPinkById(id);
    }

    /**
     * 查询拼团列表
     *
     * @param tPink 拼团
     * @return 拼团
     */
    @Override
    public List<TPink> selectTPinkList(TPink tPink)
    {
        return tPinkMapper.selectTPinkList(tPink);
    }

    /**
     * 新增拼团
     *
     * @param tPink 拼团
     * @return 结果
     */
    @Override
    public int insertTPink(TPink tPink)
    {
                tPink.setCreateTime(DateUtils.getNowDate());
            return tPinkMapper.insertTPink(tPink);
    }

    /**
     * 修改拼团
     *
     * @param tPink 拼团
     * @return 结果
     */
    @Override
    public int updateTPink(TPink tPink)
    {
                tPink.setUpdateTime(DateUtils.getNowDate());
        return tPinkMapper.updateTPink(tPink);
    }

    /**
     * 批量删除拼团
     *
     * @param ids 需要删除的拼团主键
     * @return 结果
     */
    @Override
    public int deleteTPinkByIds(String[] ids)
    {
        return tPinkMapper.deleteTPinkByIds(ids);
    }

    /**
     * 删除拼团信息
     *
     * @param id 拼团主键
     * @return 结果
     */
    @Override
    public int deleteTPinkById(String id)
    {
        return tPinkMapper.deleteTPinkById(id);
    }


    @Override
    public  List<TPink>  getPinkListByTeamId(String ptActiveId,String teamId){
        TPink  pink = new TPink();
        pink.setTeamId(teamId);
        pink.setPtActiveId(ptActiveId);
        pink.setStatusList(Arrays.asList("1","2"));
        List<TPink> list = this.selectTPinkList(pink);
        return list;
    }

    @Override
    public TPink getCurrPinkByTeamId(String ptActiveId,String teamId){
        TPink  cureent = new TPink();
        TPink team = this.getById(teamId);
        if(team.getUserId().equals(SecurityUtils.getUserIdStr())){
            cureent=  team;
        }else{
            TPink  pink = new TPink();
            pink.setTeamId(teamId);
            pink.setPtActiveId(ptActiveId);
            pink.setStatusList(Arrays.asList("1","2"));
            List<TPink> list = this.selectTPinkList(pink);
            for (TPink pinkvo : list) {
                if (pinkvo.getUserId().equals(SecurityUtils.getUserIdStr())) {
                    cureent= pinkvo;
                    break;
                }
            }
        }
        return cureent;
    }




    @Override
    public boolean pinkSuccess(String teamId) {
        if (ObjectUtil.isNull(teamId)) {
            return false;
        }
        TPink teamPink = this.getById(teamId);
        List<TPink> memberList = getPinkListByTeamId(teamPink.getPtActiveId(), teamId);
        memberList.add(teamPink);
        memberList.forEach(i -> {
            i.setStatus("2");
            i.setEndTime(DateUtils.getNowDate());
        });
        return updateBatchById(memberList);
    }

    /**
     * 判断用户是否在团内
     *
     * @return boolean true=在
     */
    @Override
    public boolean getUserIsPink(String pinkId, String userId) {
        Long count = this.lambdaQuery()
                .eq(TPink::getUserId,userId)
                .and(i->i.eq(TPink::getTeamId,pinkId).or().eq(TPink::getId,pinkId))
                .count();
        return count > 0;
    }
}
